package com.chen.demo.javase.juc.copyonwrite;

import org.omg.CORBA.TIMEOUT;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @author chenhongwei
 * @date 2019/8/31 18:11
 **/
public class CopyOnWriteArrayListDemo {
    private static Integer shareVar = 0;


    private static Object waitObj = new Object();

    private static volatile CopyOnWriteArrayList<Integer> copyOnWriteArrayList = new CopyOnWriteArrayList<Integer>();

    public static void main(String[] args) throws InterruptedException {
        copyOnWriteArrayList.add(1);
        copyOnWriteArrayList.add(2);
        copyOnWriteArrayList.add(3);
        copyOnWriteArrayList.add(4);
        copyOnWriteArrayList.add(5);
        //在线程启动之前获得迭代器
        Iterator<Integer> iterator = copyOnWriteArrayList.iterator();

        Thread task1 = new Thread(new Runnable() {
            public void run() {
                copyOnWriteArrayList.remove(1);
                System.out.println("删除索引为1的元素");
            }
        });
        task1.start();
        //等待线程1执行完成
        task1.join();

        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }



    }

}
